package com.jinglin.web.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jinglin.controller.ABaseController;
import com.jinglin.entity.dto.TokenUserInfoDto;
import com.jinglin.entity.dto.UserMessageCountDto;
import com.jinglin.entity.enums.MessageReadTypeEnum;
import com.jinglin.entity.po.UserMessage;
import com.jinglin.entity.query.UserMessageQuery;
import com.jinglin.entity.vo.PaginationResultVO;
import com.jinglin.entity.vo.ResponseVO;
import com.jinglin.service.UserMessageService;
import com.jinglin.web.annotation.GlobalInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 * 用户消息表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2025-04-06
 */
@RestController
@RequestMapping("/message")
public class UserMessageController extends ABaseController {
    @Autowired
    private UserMessageService userMessageService;
    @RequestMapping("/loadMessage")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO loadMessage(@NotNull Integer messageType, Integer pageNo) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        PaginationResultVO paginationResultVO = userMessageService.loadMessage(messageType,tokenUserInfoDto.getUserId(), pageNo);
        return  getSuccessResponseVO(paginationResultVO);
    }
    @RequestMapping("/getNoReadCount")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO getNoReadCount() {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        if (tokenUserInfoDto == null) {
            return getSuccessResponseVO(0);
        }
        Integer count = userMessageService.countGroup(tokenUserInfoDto.getUserId(),MessageReadTypeEnum.NO_READ.getType());
        return getSuccessResponseVO(count);
    }

    @RequestMapping("/getNoReadCountGroup")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO getNoReadCountGroup() {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        List<UserMessageCountDto> dataList = userMessageService.getMessageTypeNoReadCount(tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(dataList);
    }

    @RequestMapping("/readAll")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO readAll(Integer messageType) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();
        userMessageService.readAll(messageType,tokenUserInfoDto.getUserId());
        return getSuccessResponseVO(null);
    }


    @RequestMapping("/delMessage")
    @GlobalInterceptor(checkLogin = true)
    public ResponseVO delMessage(@NotNull String messageId) {
        TokenUserInfoDto tokenUserInfoDto = getTokenUserInfoDto();

        LambdaQueryWrapper<UserMessage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserMessage::getUserId,tokenUserInfoDto.getUserId());
        wrapper.eq(UserMessage::getMessageId,messageId);
        userMessageService.remove(wrapper);
        return getSuccessResponseVO(null);
    }

}

